#include "addstu.h"
#include "student.h"
#include "ui_addstu.h"

#include <QMessageBox>  //提示窗功能
#include <QDebug>
#include <QPushButton>
#include <QCursor>
/**/
#include <QAbstractButton>
#include <QCheckBox>
#include <QIODevice>
#include <QTextStream>
/**/

addStu::addStu(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::addStu)
{
    ui->setupUi(this);
    setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint);
    this->clearAddStuInterface();

    QSqlQuery query;
    query.exec("SELECT *FROM academy");
    while(query.next()) //获得学院下拉菜单
        this->ui->addStu_academy->addItem(query.value("Academy").toString());
}

addStu::~addStu()
{
    delete ui;
}

void addStu::on_addStu_ok_clicked()
{
    //用户按下确定时读取表单的内容
    QString name = this->ui->addStu_name->text();
    QString id = this->ui->addStu_id->text();
    QString sex = this->ui->sexGroup->checkedButton()->text();
    QString tel = this->ui->addStu_tel->text();
    QString grade = this->ui->addStu_grade->currentText();
    QString classnum = this->ui->addStu_class->currentText();
    QString academy = this->ui->addStu_academy->currentText();
    QString major = this->ui->addStu_major->currentText();
    QString politic = this->ui->politicGroup->checkedButton()->text();
    QString content = "姓名： " + name + "\n" + "学号： " + id + "\n" + "性别： " + sex + "\n" + "电话： " + tel + "\n" + "年级： "
            + grade + "\n" + "班级： " + classnum + "\n" + "学院： " + academy + "\n" + "专业： " + major + "\n" + "政治面貌： " + politic;

    QSqlQuery search;
    setCursor(Qt::BusyCursor);
    //返回学院、专业、政治面貌代码

    search.exec(QString("SELECT *FROM policy WHERE Policy = '%1'").arg(politic));
    search.next();
    QString Type = search.value("Type").toString();

    search.exec(QString("SELECT *FROM major WHERE Major= '%1'").arg(major));
    search.next();
    QString MID = search.value("MID").toString();
    QString AID = search.value("AID").toString();

    //姓名检测（1~5个汉字）
    bool name_error = !name.contains(QRegExp("^[\u4E00-\u9FA5]{1,5}$")); //全中文为false
    if(name.length() < 1)
        name_error = true;
    //qDebug() << "name" << name_error;

    //tel检测(11位纯数字)
    bool tel_error = tel.contains(QRegExp("[\\x20-\\x2f | \\x3a-\\x7e]+"));  //含非数字为true
    if(tel.length() != 11)
        tel_error = true;
    //qDebug() << "tel" << tel_error;

    //学号检测(首位大写字母+9位纯数字)
    bool id_error;
    if(id.length() != 10)
        id_error = true;
    else{
        id_error = id.right(9).contains(QRegExp("[\\x20-\\x2f | \\x3a-\\x7e]+"));  //后九位
        if(id[0] < 65 || id[0] > 90)    //首字符不为大写字母
            id_error = true;
    }
    //qDebug() << "UID" << id_error;

    if(name_error){
        setCursor(Qt::ArrowCursor);
        QMessageBox::critical(this, "ERROR", "姓名填写错误（1~5个汉字）", "确定");
    }
    else if(tel_error){
        setCursor(Qt::ArrowCursor);
        QMessageBox::critical(this, "ERROR", "电话填写错误（11位纯数字）", "确定");
    }
    else if(id_error){
        setCursor(Qt::ArrowCursor);
        QMessageBox::critical(this, "ERROR", "学号格式错误（大写字母+9位纯数字）", "确定");
    }
    else{
        setCursor(Qt::ArrowCursor);
        int ret = QMessageBox::warning(this, "请确认信息", content, "确认", "取消");
        if(!ret){
            setCursor(Qt::BusyCursor);
            search.exec(QString("SELECT *FROM students WHERE UID = '%1'").arg(id));
            if(search.next()){
                setCursor(Qt::ArrowCursor);
                QMessageBox::critical(this, "ERROR", "学生已存在，请勿重复添加", "确定");
            }
            else{
                QString insert_user = QString("INSERT INTO users (Account, Password) VALUES ('%1', '%2')").arg(id).arg(id);
                QString insert_stu = QString("INSERT INTO students (Name, UID, Sex, Politics, Academy, Major, "
                                             "Grade, Class, Phone) VALUES "
                                             "('%1', '%2', '%3', %4, %5, %6, %7, %8, '%9')").arg(name)
                                             .arg(id).arg(sex).arg(Type).arg(AID).arg(MID).arg(grade).arg(classnum).arg(tel);
                QSqlQuery insert;
                if(insert.exec(insert_stu) == false){
                    setCursor(Qt::ArrowCursor);
                    QMessageBox::critical(this, "ERROR", "所填信息不合法", "确定");
                }
                else{
                    insert.exec(insert_user);
                    setCursor(Qt::ArrowCursor);
                    QMessageBox::information(this, "SUCCESS", "添加成功", "确定");
                }
            }
            clearAddStuInterface();
        }
    }
}

void addStu::clearAddStuInterface(){
    //清空输入栏界面,方便继续添加
    this->ui->addStu_name->clear();
    this->ui->addStu_id->clear();
    this->ui->addStu_male->setChecked(true);
    this->ui->addStu_tel->clear();
    this->ui->addStu_grade->setCurrentText(nullptr);
    this->ui->addStu_class->setCurrentIndex(0);
    this->ui->addStu_academy->setCurrentText(nullptr);
    this->ui->addStu_major->setCurrentIndex(0);
    this->ui->addStu_name->setFocus();
    this->ui->radioButton_6->setChecked(true);
    //隐藏专业下拉框
    //this->ui->label_7->setVisible(false);
    //this->ui->addStu_major->setVisible(false);
}

void addStu::on_addStu_quit_clicked()
{
    setCursor(Qt::BusyCursor);
    menu->displayall();
    setCursor(Qt::ArrowCursor);
    this->close();
}

void addStu::on_addStu_academy_currentTextChanged(const QString &arg1)  //下拉菜单关联
{
    qDebug() << arg1;
    setCursor(Qt::BusyCursor);
    QSqlQuery query;
    query.exec(QString("SELECT *FROM academy WHERE Academy = '%1'").arg(arg1));
    query.next();
    QString AID = query.value("AID").toString();

    query.exec(QString("SELECT *FROM major WHERE AID = %1").arg(AID));
    this->ui->addStu_major->clear();
    //this->ui->label_7->setVisible(true);
    //this->ui->addStu_major->setVisible(true);
    while(query.next()) //获得专业下拉菜单
        this->ui->addStu_major->addItem(query.value("Major").toString());
    this->ui->addStu_major->setCurrentIndex(0);
    setCursor(Qt::ArrowCursor);
}
